//
//  SettingSpeedViewController.m
//  HandyDialer
//
//	Developed by Team Synergy
//  Created by Jordan Tseng on 11/11/14.
//  Coders: Jacky Li and Jordan Tseng 
//  Graphic Designer: Derek Fong
//  Developers: Brad Lisson, Derek Fong, Jacky Li, Jeremy Gangoso, Jordan Tseng
//  Copyright HandyDialer 2011. All rights reserved.
//

#import "SettingSpeedViewController.h"

@implementation SettingSpeedViewController
@synthesize highlightArea;
@synthesize backgroundArea;
@synthesize callNumber;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
    {
        // Custom initialization
		rowCounter = 0;
		columnCounter = 0;
		iterationCounter = 0;
		dotCounter = 0;
		buttonSelect = FALSE;
    }
    return self;
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad 
{
	defaults = [NSUserDefaults standardUserDefaults];
    speed = [defaults floatForKey:@"Speed"];
	speedNumber.text = [NSString stringWithFormat:@"%f", speed];
	speedNumber.textColor = [UIColor darkGrayColor];
	highlightColor = [defaults stringForKey:@"Highlight"];
	themeColor = [defaults stringForKey:@"Theme"];
    [backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_speed_default.png"]]];
	[super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated
{
    defaults = [NSUserDefaults standardUserDefaults];
    speed = [defaults floatForKey:@"Speed"];
	highlightColor = [defaults stringForKey:@"Highlight"];
	themeColor = [defaults stringForKey:@"Theme"];
    [backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_speed_default.png"]]];
	if (uniTimer == nil && buttonSelect == FALSE)
		uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(rowChange) userInfo:nil repeats:YES];
	else if (uniTimer == nil && buttonSelect == TRUE)
		uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(buttonChange) userInfo:nil repeats:YES];
	[super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	if (uniTimer != nil)
	{
		[uniTimer invalidate];
		uniTimer = nil;
	}
	[super viewDidDisappear:animated];
}

- (void)dealloc 
{
	[number release];
	[highlightArea release];
    [super dealloc];
}

// decides which row to highlight
- (void)highlightChange:(NSString *)picture
{
    UIImage *image = [UIImage imageNamed: picture];
    [highlightArea setImage:image];
    
}

- (void)appendNum:(int)input
{
	NSString *charList[12] = {@"1", @"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",@".",nil};
	if (number == NULL)
	{
		number = charList[input];
	}
	else
	{
		number = [number stringByAppendingString:charList[input]];
	}
	
	[number retain];
	speedNumber.text = number;
	speedNumber.textColor = [UIColor blackColor];
	NSLog(@"%@",number);
	
}

- (IBAction)switchView:(id)sender
{
    // Does nothing if row counter is 0
	if(rowCounter != 0)
	{
        // Check if button is being selected
		if(buttonSelect == true && columnCounter != 0)
		{
            // Stops timer and reset iteration counter
			[uniTimer invalidate];
			uniTimer = nil;
			iterationCounter = 0;
			
            // Special case for row 4
			if(rowCounter == 4)
			{
				switch (columnCounter)
				{
					// Back space
					case 1:
					{
                        if (number != nil)
                        {
                            if ([number substringFromIndex:[number length] - 1] == @".")
                            {
                                dotCounter -= 1;
                            }
                        }
						if([number length] == 1)
							number = NULL;
						else
							number = [number substringToIndex:[number length] - 1];
						speedNumber.text = number;
						rowCounter= 0;
						columnCounter = 0;
						buttonSelect = false;
						[self rowChange];
						uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(rowChange) userInfo:nil repeats:YES];
						NSLog(@"%@",number);
						break;
					}
					// 0
					case 2:
					{
                        [self appendNum:(9)];
                        rowCounter= 0;
						columnCounter = 0;
                        buttonSelect = false;
						[self rowChange];
						uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(rowChange) userInfo:nil repeats:YES];
						break;
					}
					// Clear
					case 3:
					{
						number = NULL;
						speedNumber.text =  number;
						rowCounter= 0;
						columnCounter = 0;
						buttonSelect = false;
						[self rowChange];
						uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(rowChange) userInfo:nil repeats:YES];
						NSLog(@"%@",number);
						break;
					}
				}
			}
            // Case for row 5
			else if(rowCounter == 5)
			{
				switch (columnCounter)
				{
					// Set speed
					case 1:
                    {
                        
                        rowCounter= 0;
                        columnCounter = 0;
						buttonSelect = false;
                        // Pass number to ResultSearchDirectoryViewController and search
						if (number != nil)
						{
                            if ([number floatValue] >= 0.5 && [number floatValue] <= 9)
                            {
                                speed = [number floatValue];
                                [defaults setFloat:speed forKey:@"Speed"];
                                [defaults synchronize];
                                [self dismissModalViewControllerAnimated:NO];
								[number release];
                            }
                            else
                            {
                                InvalidInputViewController *myViewController = [[InvalidInputViewController alloc]
                                                                                initWithNibName:@"InvalidSpeedView"
                                                                                bundle:nil];
                                [self presentModalViewController:myViewController animated:NO]; 
                                dotCounter = 0;
								number = nil;
                                speedNumber.text = nil;
                                [highlightArea setImage:nil];
                            }
							
						}
                        // do nothing if no changes
						else
						{
							[self dismissModalViewControllerAnimated:NO];
							[number release];
						}
                        
						break;
                    }
					// .
                    case 2:
                    {
                        if (dotCounter < 1)
                        {
                            [self appendNum:(10)];
                            rowCounter= 0;
                            columnCounter = 0;
                            dotCounter += 1;
                            buttonSelect = false;
                            [self rowChange];
                            uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(rowChange) userInfo:nil repeats:YES];
                        }
                        else
                        {
							InvalidInputViewController *myViewController = [[InvalidInputViewController alloc]
                                                                            initWithNibName:@"InvalidSpeedView"
                                                                            bundle:nil];
							[self presentModalViewController:myViewController animated:NO]; 
							;
							[highlightArea setImage:nil];
                            rowCounter= 0;
                            columnCounter = 0;
                            buttonSelect = false;
                        }
						break;
                    }
					// Back to main menu
					case 3:
                    {
                        rowCounter= 0;
                        columnCounter = 0;
						[self dismissModalViewControllerAnimated:NO];
						[number release];
						break;
                    }
				}
			}
            // Button select for number buttons
			else
			{
				NSLog(@"column:%d row:%d",columnCounter,rowCounter);
				[self appendNum:(columnCounter-1+(rowCounter-1)*3)];
				rowCounter= 0;
				columnCounter = 0;
				buttonSelect = false;
				[self rowChange];
				uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(rowChange) userInfo:nil repeats:YES];
			}
		}
        // Case for row select
		else
		{
			[uniTimer invalidate];
			uniTimer = nil;
			buttonSelect = true;
			columnCounter = 0;
			[self buttonChange];
			uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(buttonChange) userInfo:nil repeats:YES];
		}
	}
}

// Decides which row to go to
-(void)rowChange
{
    
	if( rowCounter == 5)
	{
		rowCounter = 0;
	}
	rowCounter += 1;
	NSString *next = highlightColor;
    next = [next stringByAppendingString:@"_dialer_row"]; 
	next = [next stringByAppendingString:[NSString stringWithFormat:@"%d", rowCounter]];
	next = [next stringByAppendingString:@".png"];
	[self highlightChange:next];
    
    
}

// decides which button should be displayed next
-(void)buttonChange 
{
    if (columnCounter == 3)
    {
        columnCounter = 0;
        iterationCounter += 1;
    }
	if(iterationCounter == 3)
	{
		[uniTimer invalidate];
		uniTimer = nil;
		iterationCounter = 0;
		columnCounter = 0;
		rowCounter = 0;
		buttonSelect = false;
		[self rowChange];
		uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(rowChange) userInfo:nil repeats:YES];
		
	}
	else
	{
		columnCounter += 1;
        NSString *next = highlightColor;
		if(rowCounter == 5)
        {
			next = [next stringByAppendingString:@"_speed_"];
        }
		else
        {
			next = [next stringByAppendingString:@"_dialer_"];
        }
		next = [next stringByAppendingString:[NSString stringWithFormat:@"%d", columnCounter+(rowCounter-1)*3]];
		next = [next stringByAppendingString:@".png"];
		[self highlightChange:next];
	}
    
}

@end

